<?php

class indexController extends Controller {

    public $initphp_list = array(
        'login',
        'logout',
        'validcode'
    );

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    public function run() {
        $this->view->assign("menu_now", "function");
        $pro_count=$this->getProduct()->count();
        $news_count=$this->getNews()->count();
        $file_count=$this->getDownload()->count();
        $this->view->assign("pro_count", $pro_count);
        $this->view->assign("news_count", $news_count);
        $this->view->assign("file_count", $file_count);
        $this->view->set_tpl("manage/header"); // 设置公共头部模板
        $this->view->set_tpl("manage/index");
        $this->view->set_tpl("manage/footer"); // 设置公共尾部模板
        $this->view->assign("title", "凯巴斯-管理后台");
        $this->view->display();
    }

    /**
     * 登陆
     */
    public function login() {
        $info = $this->controller->get_gp(array(
            'username',
            'password'
        ));
        if ($info['username']) {
            $session = $this->getUtil('session');
            $password = md5($info['password']);
            $admin = InitPHP::getService("admin");
            $result = $admin->getOne($info['username']);
            if ($result) {
                if ($result['pass'] === $password) {
                    $session->set('adminid', intval($result['uid']));
                    $session->set('adminname', htmlspecialchars($info['username']));
                    $function = $this->getLibrary("function");
                    $function->goto_url("/manage");
                } else {
                    JsMessage("密码错误！");
                }
            } else {
                JsMessage("用户名不存在！");
            }
        } else {
            $this->view->set_tpl("manage/login");
            $this->view->assign("title", "活动寻管理后台");
            $this->view->display();
        }
    }

    /**
     * 检查登陆状态
     */
    public function checkLogin() {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
    }

    /**
     *
     * @return pagerInit
     */
    private function getPagerLibrary() {
        return InitPHP::getLibrarys("pager");
    }
    /**
     * 
     * @return productService
     */
    private function getProduct(){
        return InitPHP::getService("product");
    }
    /**
     * 
     * @return newsService
     */
    private function getNews(){
        return InitPHP::getService("news");
    }
    
    /**
     * 
     * @return downloadService
     */
    private function getDownload(){
        return InitPHP::getService("download");
    }

    /**
     * 获取验证码函数
     */
    public function validcode() {
        $captcha = self::getCaptcha();
        $code = $captcha->CreateCheckCode();
        $session = $this->getUtil('session');
        $session->set('user_vaildcode_key', $code);
        $captcha->OutCheckImage();
    }

    /**
     *
     * @return captchaInit
     */
    private function getCaptcha() {
        return InitPHP::getLibrarys("captcha");
    }

    /**
     * 退出后台 删除session
     */
    public function logout() {
        $session = $this->getUtil('session');
        $function = $this->getLibrary('function');
        $session->clear();
        $function->goto_url('/manage/login');
    }
}